using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.NBrightEspace.Components;
using DotNetNuke.Services.Exceptions;
using Microsoft.ApplicationBlocks.Data;
using NBrightCore.common;
using NBrightCore.render;
using NBrightDNN;

namespace DotNetNuke.Modules.NBrightEspace
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditNBrightEspace class is used to manage content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Reports : EspaceModuleBase
    {

        #region Event Handlers

        private const string EntityTypeCode = "REPORTS"; // change for DB records

        override protected void OnInit(EventArgs e)
        {
            //Set base EntityTypeCode, MUST be done for all NBright child ctrls before OnInit
            base.EntityTypeCode = EntityTypeCode;
            
            base.OnInit(e);
            
            //initalize the repeater template based on what we want to see, a list or editing details.
            if (base.ItemId=="")
            {// itemid is blank so display the list
                rpData.Visible = false;

                //All list display functions are processed via the parent class, we just initiallize here.
                base.OnInitActivateList(true, true, MapPath(ControlPath));

            }
            else
            {// itemid is not blank, so display the detail template.
                rpData.ItemTemplate = new GenXmlTemplate(TemplCtrl.GetTemplateData(EntityTypeCode + "_Detail.html", Utils.GetCurrentCulture()));
            }

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (Page.IsPostBack == false)
            {
                PageLoad();
            }
        }

        private void PageLoad()
        {
            try
            {
                if (base.ItemId=="")
                {// display list    

                    //set default filter
                    if (String.IsNullOrEmpty(UInfo.SearchClearAfter))
                    {
                        var strOrderBy = GenXmlFunctions.GetSqlOrderBy(CtrlSearch);
                        UInfo.SearchOrderby = strOrderBy;
                        UInfo.Save();
                    }
                    base.BindListData();
                }
                else
                {// display the detail
                    base.DoDetail(rpData);
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }



        #endregion

        #region  "Events "

        protected void CtrlItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var cArg = e.CommandArgument.ToString();

            switch (e.CommandName.ToLower())
            {
                case "new":
                    Response.Redirect(EditUrl("itemid", "0", EntityTypeCode), true);
                    break;
                case "update":
                    UpdateData();
                    Response.Redirect(EditUrl("itemid", base.ItemId, EntityTypeCode), true);
                    break;
                case "cancel":
                    Response.Redirect(EditUrl(EntityTypeCode));
                    break;
                case "delete":
                    base.DeleteData(rpData);
                    Response.Redirect(EditUrl(EntityTypeCode));
                    break;
            }

        }

        public override void EventListItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var cArg = e.CommandArgument.ToString();
            switch (e.CommandName.ToLower())
            {
                case "downloadsql":
                    var strOut = "";
                    if (Utils.IsNumeric(cArg))
                    {
                        if (UserController.GetCurrentUserInfo().IsInRole("Manager"))
                        {
                            strOut = EspaceFunctions.GetReportCsv(cArg);
                        }
                    }
                    if (strOut != "")
                    {
                        Utils.ForceStringDownload(Context.Response,"EspaceReport.csv",strOut);
                    }
                    break;
            }
        }
        #endregion

        #region "presistance methods"


        private void UpdateData()
        {
                if (Page.IsValid)
                {
                    // Does standard update of template form
                    var objInfo = (NBrightDNN.NBrightInfo)base.UpdateDetailNoValidate(rpData, EntityTypeCode,"");


                }
        }



        #endregion


    }

}